<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>findClosestMatchingNode</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <span></span>
    <script>
        const findClosestMatchingNode = (node, selector) => {
            for (let n = node; n.parentNode; n = n.parentNode) {
                if (n.matches && n.matches(selector)) {
                    return n;
                }
            }
            return null;
        };
        console.log(findClosestMatchingNode(document.querySelector('span'), 'body'));
    </script>
</body>

</html>